using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using ImageCSharp.Core.Models;

namespace ImageCSharp.Converters
{
    /// <summary>
    /// 布尔值到可见性转换器
    /// </summary>
    public class BooleanToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool boolValue)
            {
                return boolValue ? Visibility.Visible : Visibility.Collapsed;
            }
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Visibility visibility)
            {
                return visibility == Visibility.Visible;
            }
            return false;
        }
    }

    /// <summary>
    /// 空值到可见性转换器
    /// </summary>
    public class NullToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 布尔值到字体重量转换器
    /// </summary>
    public class BooleanToFontWeightConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is bool boolValue)
            {
                return boolValue ? FontWeights.Bold : FontWeights.Normal;
            }
            return FontWeights.Normal;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 颜色到画刷转换器
    /// </summary>
    public class ColorToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is System.Drawing.Color color)
            {
                return new SolidColorBrush(Color.FromArgb(color.A, color.R, color.G, color.B));
            }
            return Brushes.Transparent;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 工具类型到图标转换器
    /// </summary>
    public class ToolTypeToIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is ToolType toolType)
            {
                return toolType switch
                {
                    ToolType.Selection => "CursorDefault",
                    ToolType.Drawing => "Brush",
                    ToolType.Measurement => "Ruler",
                    ToolType.Transform => "Transform",
                    ToolType.Filter => "Filter",
                    _ => "Help"
                };
            }
            return "Help";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// 工具类型到可见性转换器
    /// </summary>
    public class ToolTypeToVisibilityConverter : IValueConverter
    {
        // 将内部局部变量名从 targetType 改为 targetToolType（避免与参数名冲突）
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 1. 重命名局部变量，解决命名冲突
            if (value is ToolType toolType && parameter is string targetToolType)
            {
                // 2. 优化：使用 Enum.TryParse 替代 Enum.Parse，避免参数错误导致崩溃
                if (Enum.TryParse<ToolType>(targetToolType, out var expectedType))
                {
                    return toolType == expectedType ? Visibility.Visible : Visibility.Collapsed;
                }
            }
            // 解析失败/参数异常时默认隐藏
            return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // 如需双向绑定，可补充逻辑；否则保持 NotImplementedException
            throw new NotImplementedException("ToolTypeToVisibilityConverter 不支持反向转换");
        }
    }
}